print("文件被加载")

import os
import subprocess
import time
import xml.etree.ElementTree as ET
import re
import logging

logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

def open_didi_app():
    """使用ADB命令打开滴滴出行APP"""
    try:
        logger.debug("正在检查ADB设备连接...")
        check_device = subprocess.run(['adb', 'devices'], capture_output=True, text=True)
        logger.debug(f"设备检查结果: {check_device.stdout}")
        if 'device' not in check_device.stdout:
            raise Exception("没有找到已连接的设备")
        
        logger.debug("尝试关闭已运行的滴滴...")
        subprocess.run(['adb', 'shell', 'am', 'force-stop', 'com.sdu.didi.psnger'])
        time.sleep(2)
        
        logger.info("正在启动滴滴出行...")
        launch_command = [
            'adb', 
            'shell', 
            'am', 
            'start', 
            '-a', 'android.intent.action.MAIN',
            '-c', 'android.intent.category.LAUNCHER',
            '-n', 'com.sdu.didi.psnger/com.didi.sdk.app.launch.splash.SplashActivity'
        ]
        
        result = subprocess.run(launch_command, capture_output=True, text=True)
        logger.debug(f"启动命令输出: {result.stdout}")
        logger.debug(f"启动命令错误: {result.stderr}")
        
        if result.returncode == 0:
            logger.info("滴滴出行启动成功")
            time.sleep(10)
            find_and_call_taxi()
        else:
            logger.error(f"启动失败: {result.stderr}")
            
    except Exception as e:
        logger.error(f"启动APP时发生错误: {str(e)}")

def get_ui_xml():
    """获取当前界面的XML布局"""
    try:
        subprocess.run(['adb', 'shell', 'uiautomator', 'dump', '/sdcard/window_dump.xml'])
        subprocess.run(['adb', 'pull', '/sdcard/window_dump.xml', '.'])
        return ET.parse('window_dump.xml').getroot()
    except Exception as e:
        logger.error(f"获取界面布局失败: {str(e)}")
        return None

def find_and_call_taxi():
    """查找并点击呼叫按钮"""
    try:
        logger.info("正在点击呼叫按钮...")
        
        # 屏幕中央坐标
        x = 1080 // 2  # 540
        y = 2340 // 2  # 1170
        
        # 点击屏幕中央
        subprocess.run([
            'adb',
            'shell',
            f'input tap {x} {y}'
        ])
        
        logger.info("已点击呼叫按钮")
        time.sleep(2)
        
    except Exception as e:
        logger.error(f"点击呼叫按钮失败: {str(e)}")

def cleanup_files():
    """清理临时文件"""
    try:
        if os.path.exists('window_dump.xml'):
            os.remove('window_dump.xml')
        subprocess.run(['adb', 'shell', 'rm', '/sdcard/window_dump.xml'])
    except Exception as e:
        logger.error(f"清理文件时发生错误: {str(e)}")

if __name__ == "__main__":
    open_didi_app()
